﻿using log4net.Repository.Hierarchy;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.BackgroundJobs;
using Volo.Abp.DependencyInjection;
using WorkFlowCore.Authorization;
using WorkFlowCore.EventData;
using WorkFlowCore.WorkTasks;

namespace WorkFlowCore.Framework.BackgroundJobs
{
    public class AutoHandleStepsBackgroundJob : AsyncBackgroundJob<AutoHandleStepsEventData>, IScopedDependency
    {
        private readonly WorkTaskManager workTaskManager;
        private readonly IWorkflowSession session;
        public readonly ILogger<AutoHandleStepsBackgroundJob> logger;

        public AutoHandleStepsBackgroundJob(WorkTaskManager workTaskManager, IWorkflowSession session, ILogger<AutoHandleStepsBackgroundJob> logger)
        {
            this.workTaskManager = workTaskManager;
            this.session = session;
            this.logger = logger;
        }

        public override async Task ExecuteAsync(AutoHandleStepsEventData args)
        {
            logger.LogDebug("job test");
            session.SetUser(args.User);
            if (args == null || args.Steps == null) return;
            foreach (var step in args.Steps)
            {
                await workTaskManager.PassApprove(step.Id, args.Comment, string.Empty);
            }
        }
    }
}
